<script setup>
import {ref, reactive} from 'vue';
import {useRouter} from 'vue-router';
import {ElForm, ElFormItem, ElInput, ElButton, ElMessage, ElIcon} from 'element-plus';
import {Lock} from '@element-plus/icons-vue';
// 导入用户密码修改接口
import userApi from '../api/user_api.js';

const router = useRouter();

// 1. 表单数据
const formData = ref({
  user_code: '',    // 原密码
  newPassword: '',    // 新密码
  confirmPassword: '' // 确认新密码
});

// 2. 表单引用与验证规则（简化：仅保留基础校验）
const passwordForm = ref(null);
const formRules = {
  user_code: [
    {required: true, message: '请输入原密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度需在6-20位之间', trigger: 'blur'}
  ],
  newPassword: [
    {required: true, message: '请输入新密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度需在6-20位之间', trigger: 'blur'}
  ],
  confirmPassword: [
    {required: true, message: '请确认新密码', trigger: 'blur'},
    {
      validator: (rule, value, callback) => {
        console.log(formData.value.newPassword,formData.value.confirmPassword)
        if (formData.value.newPassword !== formData.value.confirmPassword) {
          callback(new Error('两次输入的新密码不一致123'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ]
};

// 4. 提交密码修改
const handleSubmit = async () => {
  try {
    // 表单基础验证
    await passwordForm.value.validate();

    // 调用接口提交（原密码+新密码+）
    const result = await userApi.changePassword(formData.value);

    // 修改成功处理
    if (result) {
      ElMessage.success('密码修改成功！请重新登录');
      // 重置表单并跳转登录页
      passwordForm.value.resetFields();
      setTimeout(() => router.push('/userLogin'), 1500);
    }
  } catch (error) {
    // 错误区分：表单验证错误不额外提示（Element自动处理）
    if (!(error.name === 'Error' && error.message.includes('validation'))) {
      ElMessage.error(error.message || '修改失败，请检查原密码是否正确');
    }
  }
};

// 5. 重置表单
const handleReset = () => {
  passwordForm.value.resetFields();
  ElMessage.info('表单已重置');
};

// 6. 返回上一页
const goBack = () => {
  router.go(-1);
};
</script>

<template>
  <div class="change-pwd-container">
    <!-- 背景图（与系统风格统一） -->
    <div class="bg-image"></div>

    <!-- 密码修改卡片 -->
    <el-card class="change-pwd-card" shadow="hover">
      <div class="card-header">
        <!-- 头部图标 -->
        <div class="header-icon">
          <ElIcon class="lock-icon">
            <Lock/>
          </ElIcon>
        </div>
        <h2 class="header-title">修改密码</h2>
      </div>

      <!-- 密码修改表单 -->
      <el-form
          ref="passwordForm"
          :model="formData"
          :rules="formRules"
          label-width="120px"
          class="password-form"
      >
        <!-- 原密码 -->
        <el-form-item label="原密码" prop="user_code">
          <el-input
              v-model="formData.user_code"
              type="password"
              placeholder="请输入原密码"
              maxlength="20"
          >
          </el-input>
        </el-form-item>

        <!-- 新密码 -->
        <el-form-item label="新密码" prop="newPassword">
          <el-input
              v-model="formData.newPassword"
              type="password"
              placeholder="请输入6-20位新密码"
              maxlength="20"
          >
          </el-input>
        </el-form-item>

        <!-- 确认新密码 -->
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
              v-model="formData.confirmPassword"
              type="password"
              placeholder="请再次输入新密码"
              maxlength="20"
          >
          </el-input>
        </el-form-item>

        <!-- 操作按钮 -->
        <el-form-item class="form-actions">
          <el-button type="default" @click="goBack">返回</el-button>
          <el-button type="default" @click="handleReset">重置</el-button>
          <el-button type="primary" @click="handleSubmit" class="submit-btn">
            确认修改
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
/* 页面容器 */
.change-pwd-container {
  position: relative;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background-color: #f9f9f9;
}

/* 背景图（复用系统背景） */
.bg-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('@/assets/aa.gif') no-repeat center center;
  background-size: cover;
  z-index: 1;
  filter: brightness(0.9) opacity(0.8);
}

/* 密码修改卡片 */
.change-pwd-card {
  width: 100%;
  max-width: 500px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  padding: 30px;
  z-index: 2;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

/* 卡片头部 */
.card-header {
  text-align: center;
  margin-bottom: 25px;
}

.header-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #a5dee5 0%, #ffb6b9 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 12px;
}

.lock-icon {
  color: white;
  font-size: 28px;
}

.header-title {
  font-size: 22px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

/* 按钮区域 */
.form-actions {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 20px;
}

.submit-btn {
  background: linear-gradient(135deg, #a5dee5 0%, #ffb6b9 100%);
  border: none;
}

.submit-btn:hover {
  background: linear-gradient(135deg, #8cd0d8 0%, #ff9aa2 100%);
}

/* 响应式适配 */
@media (max-width: 576px) {
  .change-pwd-card {
    padding: 20px 15px;
  }

  .form-actions {
    flex-direction: column;
  }

  .form-actions .el-button {
    width: 100%;
  }
}
</style>